/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnlPPN_SeleccionClientePrioridad.java
 *
 * Created on 20/06/2011, 22:32:52
 */
package Presentacion.ProcesarPedidoCliente;

import Negocio.Modelo.Cliente;
import Negocio.Modelo.Prioridad;
import Negocio.ProcesarPedidoCliente;
import Presentacion.SIPGUIView;
import java.util.List;
import javax.swing.JComboBox;

/**
 *
 * @author NicolasM
 */
public class pnlPPC_SeleccionClientePrioridad extends javax.swing.JPanel {

    private int nroPaso;
    private pnlProcesarPedidoCliente parent;
    private ProcesarPedidoCliente proceso;
    
    /** Creates new form pnlPPN_SeleccionClientePrioridad */
    public pnlPPC_SeleccionClientePrioridad(pnlProcesarPedidoCliente parent, ProcesarPedidoCliente proceso, int nroPaso) {
        initComponents();
        
        this.proceso = proceso;
        this.parent = parent;
        this.nroPaso = nroPaso;
        
        //Cargo los combos
        CargarComboClientes();
        CargarComboPrioridad();
    }

    @Override
    public void setVisible(boolean visible){
        super.setVisible(visible);
        
        if(visible){
            SIPGUIView.getInstance().SeleccionarNumeroPaso(nroPaso);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cboClientes = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cboPrioridad = new javax.swing.JComboBox();
        btnSiguiente = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(Presentacion.SIPGUIApp.class).getContext().getResourceMap(pnlPPC_SeleccionClientePrioridad.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cboClientes.setName("cboClientes"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        cboPrioridad.setName("cboPrioridad"); // NOI18N

        btnSiguiente.setText(resourceMap.getString("btnSiguiente.text")); // NOI18N
        btnSiguiente.setName("btnSiguiente"); // NOI18N
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cboPrioridad, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboClientes, 0, 205, Short.MAX_VALUE))
                .addContainerGap(106, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(303, Short.MAX_VALUE)
                .addComponent(btnSiguiente)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cboPrioridad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182, Short.MAX_VALUE)
                .addComponent(btnSiguiente)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        this.SiguientePaso();
    }//GEN-LAST:event_btnSiguienteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JComboBox cboClientes;
    private javax.swing.JComboBox cboPrioridad;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

    
    // <editor-fold desc="Métodos de negocio">                          
    
    private void SiguientePaso(){
        long cuit = ((ComboClienteItem)cboClientes.getSelectedItem()).getCuit();
        this.proceso.IdentificarCliente(cuit);

        int prioridad = ((ComboPrioridadItem)cboPrioridad.getSelectedItem()).getNivel();
        proceso.SetearPrioridadPedido(prioridad);

        parent.SiguientePaso(nroPaso);
        
    }
    
    private void CargarComboClientes(){
        List<Cliente> clientes = proceso.getClientes();
        for(Cliente cliente:clientes){
            cboClientes.addItem(new ComboClienteItem(cliente));
        }
    }
    
    private void CargarComboPrioridad(){
        List<Prioridad> prioridades = proceso.getPrioridades();
        for(Prioridad prioridad: prioridades){
            cboPrioridad.addItem(new ComboPrioridadItem(prioridad));
        }
    }
    
    // </editor-fold>
    
    
    class ComboClienteItem{
        private Cliente cliente;
        
        public long getCuit(){
            return cliente.getCuit();
        }
        
        public ComboClienteItem(Cliente cliente){
            this.cliente = cliente;
        }
        
        @Override
        public String toString(){
            return this.cliente.getNombre();
        }
    }

    class ComboPrioridadItem{
        private Prioridad prioridad;
        
        public int getNivel(){
            return prioridad.getNivel();
        }
        
        public ComboPrioridadItem(Prioridad prioridad){
            this.prioridad = prioridad;
        }
        
        @Override
        public String toString(){
            return prioridad.getDescripcion();
        }
    }
}
